package com.jhws.business.customer.api;

import com.alibaba.fastjson.JSON;
import com.jhws.business.advertisement.bo.AdvConstant;
import com.jhws.business.advertisement.bo.NoticeInfo;
import com.jhws.business.advertisement.bo.TblNoticeDimBo;
import com.jhws.business.advertisement.service.ITblNoticeDimService;
import com.jhws.business.comm.bo.CommInfo;
import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.customer.bo.AdvInfo;
import com.jhws.business.customer.bo.CustomOutdoorAdvInfo;
import com.jhws.business.customer.service.SdkCustService;
import com.jhws.business.device.bo.DevInfo;
import com.jhws.business.device.bo.DeviceConstant;
import com.jhws.business.owner.bo.OwnerConstant;
import com.jhws.business.owner.bo.OwnerInfo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.menu.bo.MenuConstant;
import com.jhws.util.GetId;
import com.jhws.common.base.bo.Global;
import com.jhws.core.push.PushXinGe;
import com.jhws.business.advertisement.bo.AdvJson;
import com.jhws.common.base.bo.BaseJson;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.sys.appUser.service.ITblSipAcountDimService;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.sys.cms.service.ResourceService;
import com.jhws.util.AccountsUtils;
import com.jhws.util.DateUtil;
import com.jhws.util.ObjectUtils;
import com.jhws.util.StringUtils;
import com.tencent.xinge.MessageIOS;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/20.
 */
@SuppressWarnings("Duplicates")
@Controller
@RequestMapping("/custom/syn_data")
public class CustomApi {
    @Resource
    private ITblCommDimService commDimService;
    @Resource
    private ITblOwnerDimService ownerDimService;
    @Resource
    private ITblSipAcountDimService sipAcountDimService;
    @Resource
    protected ITblUserDimService userDimService;
    @Resource
    private ResourceService rs;
    @Resource
    protected ITblBuildingDimService iTblBuildingDimService;
    @Resource
    private RedisTemplate stringTemplate;
    @Resource
    protected ITblNoticeDimService ITblNoticeDimService;
    @Resource
    protected SdkCustService sdkCustService;

    @RequestMapping("putOwner")
    @ResponseBody
    public Object putOwner(HttpServletRequest request, OwnerInfo ownerInfo) throws Exception {
        String customId = request.getHeader("customId");
        TblOwnerDimBo ownerDimBo = new TblOwnerDimBo();
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(ownerInfo.getCommName(), ownerInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("putOwner", -1, "没有 " + ownerInfo.getCommName() + " 小区");
        }
        ownerDimBo.setCommCode(commDimBo.getCommCode());
        ownerDimBo.setCommName(commDimBo.getCommName());

        if (!StringUtils.isEmpty(ownerInfo.getOwnerName())) {
            ownerDimBo.setOwnerName(ownerInfo.getOwnerName());
        } else {
            return new JsonRespData("putOwner", -1, "业主名称必须填写！");
        }
        if (!StringUtils.isEmpty(ownerInfo.getOwnerMob())) {
            ownerDimBo.setOwnerMob(ownerInfo.getOwnerMob());
        } else {
            return new JsonRespData("putOwner", -1, "业主手机必须填写！");
        }
        List<TblBuildingDimBo> buildingDimBos;
        if (isBuildingInfo(ownerInfo.getBuildingCode())) {
            if (!StringUtils.isEmpty(ownerInfo.getSectionName())) {
                buildingDimBos = iTblBuildingDimService.findTblBuildingDimByWhere(
                        "where 1=1 and comm_code ='" + commDimBo.getCommCode() +
                                "' and section_name='" + ownerInfo.getSectionName() +
                                "' and areas_in_section='" + dealBuildingData(ownerInfo.getBuildingCode()) + "' ");
                if (buildingDimBos != null && buildingDimBos.size() > 0) {
                    ownerDimBo.setAreaCode(AccountsUtils.appendZeroBuilding(buildingDimBos.get(0).getAreaName()));
                } else {
                    return new JsonRespData("putOwner", -1, "区域内没有该楼栋！");
                }
            } else {
                buildingDimBos = iTblBuildingDimService.findTblBuildingDimByWhere(
                        "where 1=1 and comm_code ='" + commDimBo.getCommCode() +
                                "' and area_name='" + dealBuildingData(ownerInfo.getBuildingCode()) + "'");
                ownerDimBo.setAreaCode(ownerInfo.getBuildingCode());
            }
        } else {
            return new JsonRespData("putOwner", -1, "栋号必须为2为数字！");
        }

        if (isBuildingInfo(ownerInfo.getUnitCode())) {
            ownerDimBo.setBuildingCode(ownerInfo.getUnitCode());
        } else {
            return new JsonRespData("putOwner", -1, "单元号必须为2为数字！");
        }
        if (isBuildingInfo(ownerInfo.getFloorCode())) {
            ownerDimBo.setUnitCode(ownerInfo.getFloorCode());
        } else {
            return new JsonRespData("putOwner", -1, "楼号必须为2为数字！");
        }
        if (isBuildingInfo(ownerInfo.getRoomCode())) {
            ownerDimBo.setRoomId(ownerInfo.getRoomCode());
        } else {
            return new JsonRespData("putOwner", -1, "房号必须为2为数字！");
        }

        ownerDimBo.setApplyType(OwnerConstant.OWNER_TYPE);
        StringBuilder roomInfoNum = new StringBuilder();
        String roomInfo = "[" + ownerDimBo.getCommName() + "]";
        //新的小区加入别名
        if (buildingDimBos != null && buildingDimBos.size() > 0) {
            roomInfo += " " + buildingDimBos.get(0).getAreaAliasName() + " ";
        } else {
            roomInfo += " " + ownerDimBo.getAreaCode() + "栋 ";
        }
        roomInfo += ownerDimBo.getBuildingCode() +
                "单元 " +
                ownerDimBo.getUnitCode() +
                "楼 " +
                ownerDimBo.getRoomId() +
                "房";
        roomInfoNum.append(ownerDimBo.getCommCode())
                .append(AccountsUtils.getCompactAeraCode(ownerDimBo.getAreaCode()))
                .append(ownerDimBo.getBuildingCode())
                .append(ownerDimBo.getUnitCode())
                .append(ownerDimBo.getRoomId());

        ownerDimBo.setRoomInfo(roomInfo);
        ownerDimBo.setRoomInfoNum(roomInfoNum.toString());
        ownerDimBo.setLastModify(DateUtil.getTimeNowFmtSec());
        ownerDimBo.setLastModifyTimeStamp(DateUtil.getTimeStampsNowFmtSec());

        /* 判断申请的业主是否已申请过 */
        String checkResult = ownerDimService.checkFormOwnerLegal(ownerDimBo);
        if (checkResult != null) {
            return new JsonRespData("putOwner", -1, checkResult);
        }
        /* 未申请过的话就添加业主sip账号 */
        String accountId = sipAcountDimService.addSipAccount(ownerDimBo.getOwnerMob(), customId,
                "mobile", false);
        /* 为该sip账号添加默认的设备绑定 */
        sipAcountDimService.addSipBinding(ownerDimBo.getOwnerMob(), roomInfoNum.toString(), Global.BINDING_TYPE_INDOOR, ownerInfo.getPstn());
        ownerDimBo.setSipAcountId(accountId);
        ownerDimBo.setOwnerCode(GetId.randomID());
        ownerDimService.save(ownerDimBo);
        return new JsonRespData("putOwner", 1, ownerDimBo.getOwnerCode());
    }

    @RequestMapping("updateOwner")
    @ResponseBody
    public Object updateOwner(HttpServletRequest request, OwnerInfo ownerInfo) throws Exception {
        String customId = request.getHeader("customId");
        TblOwnerDimBo ownerDimBo = ownerDimService.findTblOwnerDimById(ownerInfo.getOwnerCode());
        if (ownerDimBo == null) {
            return new JsonRespData("updateOwner", -1, "业主不存在！");
        }
        String oldMob = ownerDimBo.getOwnerMob();
        String oldRoomInfoNum = ownerDimBo.getRoomInfoNum();
        if (ownerInfo.getCommName() != null) {
            return new JsonRespData("updateOwner", -1, "不能修改业主所在小区，只能添加！");
        }
        if (ownerInfo.getOwnerName() != null) {
            ownerDimBo.setOwnerName(ownerInfo.getOwnerName());
        }
        if (ownerInfo.getOwnerMob() != null) {
            ownerDimBo.setOwnerMob(ownerInfo.getOwnerMob());
        }

        List<TblBuildingDimBo> buildingDimBos;
        if (StringUtils.isNotEmpty(ownerInfo.getBuildingCode())) {
            if (isBuildingInfo(ownerInfo.getBuildingCode())) {
                if (!StringUtils.isEmpty(ownerInfo.getSectionName())) {
                    buildingDimBos = iTblBuildingDimService.findTblBuildingDimByWhere(
                            "where 1=1 and comm_code ='" + ownerDimBo.getCommCode() +
                                    "' and 1=1 and section_name='" + ownerInfo.getSectionName() +
                                    "' and 1=1 and areas_in_section='" + dealBuildingData(ownerInfo.getBuildingCode()) + "'");
                    if (buildingDimBos != null && buildingDimBos.size() > 0) {
                        ownerDimBo.setAreaCode(AccountsUtils.appendZeroBuilding(buildingDimBos.get(0).getAreaName()));
                    } else {
                        return new JsonRespData("putOwner", -1, "区域内没有该楼栋！");
                    }
                } else {
                    buildingDimBos = iTblBuildingDimService.findTblBuildingDimByWhere(
                            "where 1=1 and comm_code ='" + ownerDimBo.getCommCode() +
                                    "' and area_name='" + dealBuildingData(ownerInfo.getBuildingCode()) + "'");
                    ownerDimBo.setAreaCode(ownerInfo.getBuildingCode());
                }
            } else {
                return new JsonRespData("putOwner", -1, "栋号必须为2为数字！");
            }
        } else {
            buildingDimBos = iTblBuildingDimService.findTblBuildingDimByWhere(
                    "where 1=1 and comm_code ='" + ownerDimBo.getCommCode() +
                            "' and area_name='" + dealBuildingData(ownerDimBo.getAreaCode()) + "'");
        }

        if (StringUtils.isNotEmpty(ownerInfo.getUnitCode())) {
            if (isBuildingInfo(ownerInfo.getUnitCode())) {
                ownerDimBo.setBuildingCode(ownerInfo.getUnitCode());
            } else {
                return new JsonRespData("putOwner", -1, "单元号必须为2为数字！");
            }
        }
        if (StringUtils.isNotEmpty(ownerInfo.getFloorCode())) {
            if (isBuildingInfo(ownerInfo.getFloorCode())) {
                ownerDimBo.setUnitCode(ownerInfo.getFloorCode());
            } else {
                return new JsonRespData("putOwner", -1, "楼号必须为2为数字！");
            }
        }

        if (StringUtils.isNotEmpty(ownerInfo.getRoomCode())) {
            if (isBuildingInfo(ownerInfo.getRoomCode())) {
                ownerDimBo.setRoomId(ownerInfo.getRoomCode());
            } else {
                return new JsonRespData("putOwner", -1, "房号必须为2为数字！");
            }
        }

        StringBuilder roomInfoNum = new StringBuilder();
        String roomInfo = "[" + ownerDimBo.getCommName() + "]";
        //新的小区加入别名
        if (buildingDimBos != null && buildingDimBos.size() > 0 && StringUtils.isNotEmpty(buildingDimBos.get(0).getAreaAliasName())) {
            roomInfo += " " + buildingDimBos.get(0).getAreaAliasName();
        } else {
            roomInfo += " " + ownerDimBo.getAreaCode() + "栋 ";
        }
        roomInfo += ownerDimBo.getBuildingCode() +
                "单元 " +
                ownerDimBo.getUnitCode() +
                "楼 " +
                ownerDimBo.getRoomId() +
                "房";
        roomInfoNum.append(ownerDimBo.getCommCode())
                .append(AccountsUtils.getCompactAeraCode(ownerDimBo.getAreaCode()))
                .append(ownerDimBo.getBuildingCode())
                .append(ownerDimBo.getUnitCode())
                .append(ownerDimBo.getRoomId());

        ownerDimBo.setRoomInfo(roomInfo);
        ownerDimBo.setRoomInfoNum(roomInfoNum.toString());
        ownerDimBo.setLastModify(DateUtil.getTimeNowFmtSec());
        ownerDimBo.setLastModifyTimeStamp(DateUtil.getTimeStampsNowFmtSec());

        /* 判断申请的业主是否已申请过 */
        /*String checkResult = ownerDimService.checkFormOwnerLegal(ownerDimBo);
        if (checkResult != null) {
            return new JsonRespData("putOwner", -1, checkResult);
        }*/

        /* 未申请过的话就添加业主sip账号 */
        String accountId = sipAcountDimService.addSipAccount(ownerDimBo.getOwnerMob(), customId,
                "mobile", false);
        ownerDimBo.setSipAcountId(accountId);
        //先移除原来的绑定关系
        sipAcountDimService.removeSipBinding(oldMob, oldRoomInfoNum);
        /* 为该sip账号添加默认的设备绑定 */
        sipAcountDimService.addSipBinding(ownerDimBo.getOwnerMob(), roomInfoNum.toString(), Global.BINDING_TYPE_INDOOR, ownerInfo.getPstn());
        ownerDimService.save(ownerDimBo);
        return new JsonRespData("updateOwner", 1, "修改成功");
    }

    @RequestMapping("deleteOwner")
    @ResponseBody
    public Object deleteOwner(String ownerCode) throws Exception {
        TblOwnerDimBo ownerDimBo = ownerDimService.findTblOwnerDimById(ownerCode);
        if (ownerDimBo == null) {
            return new JsonRespData("deleteOwner", -1, "业主不存在！");
        }
        sipAcountDimService.removeSipBinding(ownerDimBo.getOwnerMob(), ownerDimBo.getRoomInfoNum());
        ownerDimService.removeDim(ownerCode);
        return new JsonRespData("deleteOwner", 1, "删除成功");
    }

    @RequestMapping("assignOutdoorAdv")
    @ResponseBody
    public JsonRespData assignOutdoorAdv(HttpServletRequest request, CustomOutdoorAdvInfo outdoorAdvInfo) throws Exception {
        String customId = request.getHeader("customId");
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("putOwner", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }
        List<com.jhws.sys.cms.bo.Resource> resourceList;
        com.jhws.sys.cms.bo.Resource resource;
        if (outdoorAdvInfo.getAdvInfoList() == null) {
            return new JsonRespData("assignOutdoorAdv", -1, "最少放置1张图片广告");
        }
        List<AdvInfo> advInfoList = JSON.parseArray(outdoorAdvInfo.getAdvInfoList(), AdvInfo.class);
        for (AdvInfo advInfo : advInfoList) {
            boolean isPerms = sdkCustService.isPerms(customId, MenuConstant.PERMS_PROPERTY_ASSIGNED_ADV_8);
            if (isPerms) {
                if (advInfo.getPage() < 1 || advInfo.getPage() > 9) {
                    return new JsonRespData("assignOutdoorAdv", -1, "最多放置8张图片广告");
                }
            } else {
                if (advInfo.getPage() < 1 || advInfo.getPage() > 5) {
                    return new JsonRespData("assignOutdoorAdv", -1, "最多放置4张图片广告");
                }
            }

            String fileName;
            if ("vertical".equals(outdoorAdvInfo.getScreenType())) {
                fileName = "OutdoorAdvertisingVertical-" + advInfo.getPage();
            } else {
                fileName = "OutdoorAdvertising-" + advInfo.getPage();
            }
            resourceList = rs.queryResourceList("where 1=1 and comm_code ='" + commDimBo.getCommCode()
                    + "' and type_code ='outdoor_image_ads'"
                    + " and description='" + fileName + "'");
            if (resourceList.size() > 0) {
                resource = resourceList.get(0);
            } else {
                resource = new com.jhws.sys.cms.bo.Resource();
            }
            if (!StringUtils.isEmpty(advInfo.getImageUrl())) {
                if (resource.getIcon_url() != null && !ObjectUtils.isHttpAddress(resource.getIcon_url())) {
                    //先删除oss上的文件
                    rs.deleteRemoteFile(resource.getIcon_url());
                }
                String imagePath = advInfo.getImageUrl();
                resource.setComm_code(commDimBo.getCommCode());
                resource.setType_code("outdoor_image_ads");
                resource.setDescription(fileName);
                resource.setIsApproved(Global.CMS_APPROVED);
                resource.setApprovedState("通过审核");
                resource.setTitle("门口机广告");
                resource.setIcon_url(imagePath);
                resource.setCreator(customId);
                resource.setCreate_time(new Date());
                resource.setCustomType(outdoorAdvInfo.getScreenType());
                rs.saveResource(resource);
            } else if (resource.getResource_id() != null) {
                resource.setCreate_time(new Date());
                resource.setIsApproved(Global.CMS_NOT_APPROVED);
                resource.setApprovedState("门口机不允许显示广告");
                rs.saveResource(resource);
            }
        }
        new PushThread(iTblBuildingDimService, commDimBo.getCommCode()).start();
        return new JsonRespData("", 1, "广告设置成功");
    }

    @RequestMapping("addNotice")
    @ResponseBody
    public JsonRespData addNotice(HttpServletRequest request, CustomOutdoorAdvInfo outdoorAdvInfo, NoticeInfo noticeInfo) throws Exception {
        String customId = request.getHeader("customId");
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }
        if (StringUtils.isEmpty(noticeInfo.getTitle())) {
            return new JsonRespData("", -1, "标题不能为空");
        }
        if (StringUtils.isEmpty(noticeInfo.getContent())) {
            return new JsonRespData("", -1, "内容不能为空！");
        }
        if (StringUtils.isEmpty(noticeInfo.getStartTime())) {
            return new JsonRespData("", -1, "开始时间不能为空！");
        }
        if (StringUtils.isEmpty(noticeInfo.getEndTime())) {
            return new JsonRespData("", -1, "结束时间不能为空！");
        }

        String noticeCode = saveTblNoticeDim(noticeInfo, customId, commDimBo.getCommCode());

        AdvJson advJson = new AdvJson(null, noticeInfo.getTitle(), noticeInfo.getContent(),
                null, GetId.randomID(), "1", false);
        String s = JSON.toJSONString(new BaseJson<>(advJson, "notice", 1));
        MessageIOS messageIOS = new MessageIOS();
        HashMap<String, Object> map = new HashMap<>();
        map.put("NT", s);
        messageIOS.setCustom(map);
        messageIOS.setAlert(noticeInfo.getTitle());
        messageIOS.setExpireTime(86400);

        List<String> tagList = new ArrayList<>();
        tagList.add(commDimBo.getCommCode());
        PushXinGe.androidMultiplePushTags("OR", s, noticeInfo.getTitle(), tagList);
        PushXinGe.iosMultiplePushTags("OR", messageIOS, tagList);

        return new JsonRespData("", 1, noticeCode);
    }

    @RequestMapping("deleteNotice")
    @ResponseBody
    public JsonRespData deleteNotice(CustomOutdoorAdvInfo outdoorAdvInfo, String noticeCode) throws Exception {
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("putOwner", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }
        if (StringUtils.isEmpty(noticeCode)) {
            return new JsonRespData("", -1, "信息ID不能为空！");
        }

        ITblNoticeDimService.removeNoticeDim(noticeCode);
        return new JsonRespData("", 1, "删除成功");
    }

    @SuppressWarnings("Duplicates")
    @RequestMapping("getOnlineDevicesList")
    @ResponseBody
    public JsonRespData getOnlineDevicesList(HttpServletRequest request, CustomOutdoorAdvInfo outdoorAdvInfo) throws Exception {
        List<DevInfo> devInfoList = new ArrayList<>();
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("putOwner", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }
        HashOperations<String, String, String> hashOperations = stringTemplate.opsForHash();
        String key = "online:comm:" + commDimBo.getCommCode();
        Map<String, String> deviceMap = hashOperations.entries(key);
        for (String deviceNum : deviceMap.keySet()) {
            DevInfo devInfo = new DevInfo();
            devInfo.setCommCode(commDimBo.getCommCode());
            devInfo.setCommName(commDimBo.getCommName());
            devInfo.setDeviceNum(deviceNum);
            if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.INDOOR_TYPE) {
                devInfo.setBuildingInfo(AccountsUtils.getUnitInfo(deviceNum));
                devInfo.setDeviceName("室内机-" + deviceNum.substring(deviceNum.length() - 4, deviceNum.length()) + "房");
                devInfo.setType(DeviceConstant.INDOOR_TYPE);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.WALL_TYPE) {
                devInfo.setDeviceName("围墙机-" + deviceNum.substring(deviceNum.length() - 1, deviceNum.length()));
                devInfo.setType(DeviceConstant.WALL_TYPE);
            } else if (AccountsUtils.devicesType(deviceNum) == DeviceConstant.MANAGE_TYPE) {
                devInfo.setDeviceName("管理机-" + deviceNum.substring(deviceNum.length() - 1, deviceNum.length()));
                devInfo.setType(DeviceConstant.MANAGE_TYPE);
            } else {
                devInfo.setBuildingInfo(AccountsUtils.getUnitInfo(deviceNum));
                devInfo.setDeviceName("门口机-" + deviceNum.substring(deviceNum.length() - 1, deviceNum.length()));
                devInfo.setType(DeviceConstant.OUTDOOR_TYPE);
            }
            devInfoList.add(devInfo);
        }
        return new JsonRespData("", 1, devInfoList);
    }


    @SuppressWarnings("unchecked")
    @RequestMapping("getWebApiToken")
    @ResponseBody
    public JsonRespData getWebApiToken(HttpServletRequest request, CustomOutdoorAdvInfo outdoorAdvInfo) throws Exception {
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("getWebApiToken", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }
        String webApiToken = Global.WEB_API_TOKEN + GetId.randomID();
        ValueOperations listOperations = stringTemplate.opsForValue();
        listOperations.set(webApiToken, commDimBo.getCommCode());
        stringTemplate.expire(webApiToken, 35, TimeUnit.MINUTES);
        return new JsonRespData("", 1, webApiToken);
    }

    /**
     * 添加管理手机号
     *
     * @param outdoorAdvInfo
     * @return
     * @throws Exception
     */
    @RequestMapping("addManagerPhone")
    @ResponseBody
    public JsonRespData addManagerPhone(HttpServletRequest request, CustomOutdoorAdvInfo outdoorAdvInfo, String name, String phone) throws Exception {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone)) {
            return new JsonRespData("", -1, "参数错误！");
        }
        String customId = request.getHeader("customId");
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }

        String where = "where comm_code = '" + commDimBo.getCommCode() + "' and apply_type = '" + OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER + "'";
        List<TblOwnerDimBo> list = ownerDimService.findTblOwnerDimByWhere(where);
        if (list != null){
            if (list.size() >= 5) {
                return new JsonRespData("", -1, "最多绑定五个管理机手机号!");
            }
            for (TblOwnerDimBo tblOwnerDimBo : list){
                if (phone.equals(tblOwnerDimBo.getOwnerMob())){
                    return new JsonRespData("", -1, "该手机号码管理员已经存在!");
                }
            }
        }

        TblOwnerDimBo owner = new TblOwnerDimBo();
        owner.setOwnerCode(GetId.GetId2());

        /* 未申请过的话就添加业主sip账号 */
        String accountId = sipAcountDimService.addSipAccount(phone, customId, "mobile", false);
        owner.setSipAcountId(accountId);
        owner.setCommCode(commDimBo.getCommCode());
        owner.setCommName(commDimBo.getCommName());
        owner.setOwnerName(name);
        owner.setOwnerMob(phone);
        owner.setApplyType(OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER);
        owner.setLastModify(DateUtil.getTimeNowFmtSec());
        owner.setRoomInfoNum(commDimBo.getCommCode() + "00000001");
        ownerDimService.save(owner);
        sipAcountDimService.addSipBinding(phone, commDimBo.getCommCode() + "00000001", Global.BINDING_TYPE_MANAGER, 0);
        return new JsonRespData("", 1, "添加成功");
    }

    @RequestMapping("getCommInfo")
    @ResponseBody
    public JsonRespData getCommInfo(CustomOutdoorAdvInfo outdoorAdvInfo) throws Exception {
        TblCommDimBo commDimBo = commDimService.getCommDimBoByCommName(outdoorAdvInfo.getCommName(), outdoorAdvInfo.getCity());
        if (commDimBo == null) {
            return new JsonRespData("getCommInfo", -1, "没有 " + outdoorAdvInfo.getCommName() + " 小区");
        }
        CommInfo commInfo = new CommInfo();
        BeanUtils.copyProperties(commInfo, commDimBo);// 属性拷贝
        return new JsonRespData("", 1, commInfo);
    }

    private String saveTblNoticeDim(NoticeInfo noticeInfo, String customId, String commCode) throws Exception {
        TblNoticeDimBo bo = new TblNoticeDimBo();
        bo.setUserCode(customId);
        bo.setCreateUsr(customId);
        bo.setTitile(noticeInfo.getTitle());
        bo.setContext(noticeInfo.getContent());
        bo.setBeginDate(DateUtil.stringDate(noticeInfo.getStartTime()));
        bo.setEndDate(DateUtil.stringDate(noticeInfo.getEndTime()));
        bo.setCreateTime(new Date());
        bo.setNoticeType(AdvConstant.NOTICE_TYPE_COMM);
        bo.setNoticeCode(null);
        bo.setCommCode(commCode);
        bo.setEquType(commCode);
        return ITblNoticeDimService.save(bo).getNoticeCode();
    }

    private boolean isBuildingInfo(String bInfo) {
        return bInfo != null && ObjectUtils.isNumeric(bInfo) && bInfo.length() == 2;
    }

    class PushThread extends Thread {
        private ITblBuildingDimService iTblBuildingDimService;
        private String commCode;

        public PushThread(ITblBuildingDimService iTblBuildingDimService, String commCode) {
            this.iTblBuildingDimService = iTblBuildingDimService;
            this.commCode = commCode;
        }

        @Override
        public void run() {
            super.run();
            try {
                List<String> owList = iTblBuildingDimService.findAllOutdoorOrWallMachineByComm(commCode);
                for (String sipNum : owList) {
                    PushXinGe.androidPushSingleAccount(sipNum, "notificationUpdate", com.alibaba.fastjson.JSON.toJSONString(new JsonRespData("notificationUpdate", 1, "通知成功")));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private String dealBuildingData(String data) {
        return String.valueOf(Integer.valueOf(data));
    }
}
